package co.yixiang.modules.shop.rest;

import co.yixiang.aop.log.Log;
import co.yixiang.modules.shop.domain.ProjectInfo;
import co.yixiang.modules.shop.service.ProjectInfoService;
import co.yixiang.modules.shop.service.dto.ProjectInfoQueryCriteria;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @author lijunze
* @date 2020-05-08
*/
@Api(tags = "工程信息管理")
@RestController
@RequestMapping("/api/projectInfo")
public class ProjectInfoController {

    private final ProjectInfoService projectInfoService;

    public ProjectInfoController(ProjectInfoService projectInfoService) {
        this.projectInfoService = projectInfoService;
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('projectInfo:list')")
    public void download(HttpServletResponse response, ProjectInfoQueryCriteria criteria) throws IOException {
        projectInfoService.download(projectInfoService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询工程信息")
    @ApiOperation("查询工程信息")
    @PreAuthorize("@el.check('projectInfo:list')")
    public ResponseEntity<Object> getProjectInfos(ProjectInfoQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(projectInfoService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增工程信息")
    @ApiOperation("新增工程信息")
    @PreAuthorize("@el.check('projectInfo:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody ProjectInfo resources){
        return new ResponseEntity<>(projectInfoService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改工程信息")
    @ApiOperation("修改工程信息")
    @PreAuthorize("@el.check('projectInfo:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody ProjectInfo resources){
        projectInfoService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除工程信息")
    @ApiOperation("删除工程信息")
    @PreAuthorize("@el.check('projectInfo:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Integer[] ids) {
        projectInfoService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}